package com.example.schedulecard;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.schedulecard.DataTransfer.ScheduleNameTransfer;
import com.example.schedulecard.Databases.CourseDatabaseOption;
import com.example.schedulecard.Entity.Course;
import com.example.schedulecard.Entity.Schedule;

import java.util.List;

public class ScheduleCardHome extends AppCompatActivity {

    private ImageView addCourse, back;
//    一个获取数据库
    private SQLiteDatabase sqLiteDatabase;

    private TextView name,saturday,sunday;

    private RelativeLayout saturdays,sundays;
//    课程表中的课程数量
    private int currentCoursesNumber = 0;
//    当前课表中结束最晚的节数
    private int maxCoursesNumber = 0;

//    Intent intentjs;
//    Schedule schedule;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schedule_card_home);
        Init();
    }

//    初始化成员变量
    private void Init() {
//        找到星期六及星期日的布局
        saturday = (TextView) findViewById(R.id.liu);
        sunday = (TextView) findViewById(R.id.qi);
        saturdays = (RelativeLayout) findViewById(R.id.saturday);
        sundays = (RelativeLayout) findViewById(R.id.sunday);
//        设置布局
        switch (ScheduleNameTransfer.scheduleinfo.getWorksum()) {
            case 5:
//                隐藏星期六及星期日的布局
                    saturday.setVisibility(View.GONE);
                    sunday.setVisibility(View.GONE);
                    saturdays.setVisibility(View.GONE);
                    sundays.setVisibility(View.GONE);
                break;
            case 6:
                sunday.setVisibility(View.GONE);
                sundays.setVisibility(View.GONE);
                break;
            default:
        }
        name = (TextView) findViewById(R.id.course_list);
        name.setText(ScheduleNameTransfer.scheduleinfo.getSchedulename());
//        返回按钮
        back = (ImageView) findViewById(R.id.home_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
//        获取一个可写数据库
        sqLiteDatabase = CourseDatabaseOption.CreateCoursetable(ScheduleCardHome.this)
                .getWritableDatabase();
        addCourse = (ImageView) findViewById(R.id.add_courses);
////        接受上个页面传输的信息
//        intentjs = getIntent();
//        schedule = (Schedule) intentjs.getSerializableExtra("schedule");
        addCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentAdd = new Intent(ScheduleCardHome.this, AddCourses.class);
                startActivity(intentAdd);
            }
        });
//        加载课程信息
        loadCourse(ScheduleNameTransfer.scheduleinfo);
    }

//    加载课程信息
    public void loadCourse(Schedule schedule) {
        List<Course> courseList = CourseDatabaseOption.querycourse(sqLiteDatabase
                , schedule.getSchedulename());
//        逐步读取课程信息，并添加到视图中
        for (Course course : courseList) {
//            创建课程表左边课程节数一栏
            createCourseNumber(course);
//            添加课程信息
            createItemCourse(course);
        }
    }

//    创建课程节数视图
    public void createCourseNumber(@NonNull Course course) {
        int endNumber = course.getClassEnd();
//        保存课程节数动态显示最高
        if (endNumber > maxCoursesNumber) {
            for (int i = 0; i < endNumber - maxCoursesNumber; ++i) {
//            加载单个课程节数布局coursenumbe
                View view = LayoutInflater.from(this).inflate(R.layout.course_number, null);
//            设置控件的大小参数
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(110, 180);
//            将设置写入
                view.setLayoutParams(params);

//            将课程节数依次创建到maxCoursesNumber
                TextView classnumber = view.findViewById(R.id.class_number);
                classnumber.setText(String.valueOf(++currentCoursesNumber));
//            将此课程节数添加到课程表中
                LinearLayout coursenumber = (LinearLayout) findViewById(R.id.course_number);
                coursenumber.addView(view);
            }
//            更改最大课程节数
            maxCoursesNumber = endNumber;
        }
    }

//    根据所读课程创建对应课程视图
    public void createItemCourse(Course course) {
//        对星期几操作
        RelativeLayout day = getDayView(course.getDay());
//        一节课高度180
        int height = 180;
//        加载单个课程卡片
        View v = LayoutInflater.from(this).inflate(R.layout.course_info_view, null);
//        设置课程开始位置
        v.setY(height * (course.getClassStart() - 1));
//        根据课程的节数设置课程块的高度
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT
                , (course.getClassEnd() - course.getClassStart() + 1) * height - 8);

        GradientDrawable gradientDrawable = new GradientDrawable();
//        定义shape的样式:line:线  rectangle:矩形（圆角矩形） oval:椭圆，圆  ring:圆环
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
//        设置切角参数
        gradientDrawable.setCornerRadius(20);
//        设置背景颜色
        gradientDrawable.setColor(getResources().getColor(setColors(course.getColor())));
//        将宽高参数写入
        v.setLayoutParams(params);
//        将shape写入视图中
        v.setBackground(gradientDrawable);

//        更改背景颜色
//        v.setBackgroundResource(R.color.lilac);

//        寻找view中的course_info
        TextView courseInfo = v.findViewById(R.id.coure_info);
//        写入信息
        courseInfo.setText(course.getCourseName() + "\n"
                + course.getTeacher() + "\n" + course.getClassRoom());
//        添加点击事件
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                System.out.println("课程信息：");
//                System.out.println(course);
                Intent intent = new Intent(ScheduleCardHome.this, SeeCourseInfo.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("courseinfo", course);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
//        载入布局中
        day.addView(v);
    }

    public int setColors(int id) {
        int colors;
        switch (id) {
            case 0:
                colors = R.color.a1;
                break;
            case 1:
                colors = R.color.a2;
                break;
            case 2:
                colors = R.color.a3;
                break;
            case 3:
                colors = R.color.a4;
                break;
            case 4:
                colors = R.color.a5;
                break;
            case 5:
                colors = R.color.a6;
                break;
            case 6:
                colors = R.color.a7;
                break;
            default:
                colors = R.color.lilac;
        }
        return colors;
    }

//    获取星期几id
    public RelativeLayout getDayView(int day) {
        int dayId = 0;
        switch (day) {
            case 1:
                dayId = R.id.monday;
                break;
            case 2:
                dayId = R.id.tuesday;
                break;
            case 3:
                dayId = R.id.wednesday;
                break;
            case 4:
                dayId = R.id.thursday;
                break;
            case 5:
                dayId = R.id.friday;
                break;
            case 6:
                dayId = R.id.saturday;
                break;
            case 7:
                dayId = R.id.sunday;
                break;
        }
        return findViewById(dayId);
    }
}